package acme.bloodtime.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import modelo.IConfiguracionDAO;
import dao.api.DataObject;

/**
 * 
 * @author Alejandra Alvarez I.
 * 
 */
public class ConfiguracionDAO extends BloodTimeBaseDAO implements IConfiguracionDAO {

	public ConfiguracionDAO() {
		// Empty
	}

	@Override
	public void createTable() throws SQLException {
		StringBuffer strbuf;

		// ----------------------------------------

		dropTable();

		// ----------------------------------------

		strbuf = new StringBuffer();

		strbuf.append("CREATE TABLE ");
		strbuf.append(getTableName());
		strbuf.append(" (");
		strbuf.append(ConfiguracionDO.ID);
		strbuf.append(" SERIAL PRIMARY KEY,    ");
		strbuf.append(ConfiguracionDO.CLAVE);
		strbuf.append(" VARCHAR(10) NOT NULL,    ");
		strbuf.append(ConfiguracionDO.TIPO);
		strbuf.append(" VARCHAR(10) NOT NULL,    ");
		strbuf.append(ConfiguracionDO.VALOR);
		strbuf.append(" VARCHAR(5) NOT NULL");
		strbuf.append(")");

		System.err.println(strbuf.toString());

		connection.createStatement().execute(strbuf.toString());
	}

	@Override
	public void delete(DataObject dataObject) throws SQLException {
		internalDelete(dataObject, ConfiguracionDO.class);
	}

	@Override
	public void insert(DataObject dataObject) throws SQLException {
		checkCache(dataObject, CHECK_INSERT);
		checkClass(dataObject, ConfiguracionDO.class, CHECK_INSERT);

		ConfiguracionDO configuracionDO = (ConfiguracionDO) dataObject;

		configuracionDO.setId(getNextId());

		StringBuffer strbuf = new StringBuffer();

		strbuf.append("INSERT INTO ");
		strbuf.append(getTableName());
		strbuf.append(" VALUES (");
		strbuf.append(configuracionDO.getId());
		strbuf.append(", ");
		strbuf.append(singleQuotes(configuracionDO.getClave()));
		strbuf.append(", ");
		strbuf.append(singleQuotes(configuracionDO.getTipo()));
		strbuf.append(", ");
		strbuf.append(singleQuotes(configuracionDO.getValor()));
		strbuf.append(")");

		System.err.println(strbuf.toString());

		connection.createStatement().execute(strbuf.toString());

		dtaSession.add(dataObject);
	}

	protected ConfiguracionDO resultSetToDO(ResultSet rs) throws SQLException {
		ConfiguracionDO ret = //
		(ConfiguracionDO) dtaSession.getDtaByKey( //
				ConfiguracionDO.class, rs.getInt(ConfiguracionDO.ID));

		if (ret != null) {
			return ret;
		}

		ret = new ConfiguracionDO();

		ret.setId/*                       */(rs.getInt(ConfiguracionDO.ID));
		ret.setClave/*                   */(rs.getString(ConfiguracionDO.CLAVE));
		ret.setTipo/*              */(rs.getString(ConfiguracionDO.TIPO));
		ret.setValor/*              */(rs.getString(ConfiguracionDO.VALOR));

		return (ConfiguracionDO) dtaSession.add(ret);
	}

	@Override
	public void update(DataObject dataObject) throws SQLException {
		checkCache(dataObject, CHECK_UPDATE);
		checkClass(dataObject, ConfiguracionDO.class, CHECK_UPDATE);

		ConfiguracionDO configuracionDO = (ConfiguracionDO) dataObject;

		StringBuffer strbuf = new StringBuffer();

		strbuf.append("UPDATE ");
		strbuf.append(getTableName());
		strbuf.append(" SET ");

		strbuf.append(ConfiguracionDO.CLAVE);
		strbuf.append(" = ");
		strbuf.append(singleQuotes(configuracionDO.getClave()));

		strbuf.append(", ");

		strbuf.append(ConfiguracionDO.TIPO);
		strbuf.append(" = ");
		strbuf.append(singleQuotes(configuracionDO.getTipo()));

		strbuf.append(", ");

		strbuf.append(ConfiguracionDO.VALOR);
		strbuf.append(" = ");
		strbuf.append(singleQuotes(configuracionDO.getValor()));

		strbuf.append(" WHERE ");
		strbuf.append(ConfiguracionDO.ID);
		strbuf.append(" = ");
		strbuf.append(configuracionDO.getId());

		System.err.println(strbuf.toString());

		connection.createStatement().executeUpdate(strbuf.toString());

	}

}
